﻿<template>
	<div class="vistLogBlack-container">
		<el-dialog v-model="isShowDialog" :title="props.title" :width="800" draggable="" append-to-body="true">
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="黑名单姓名" prop="name">
							<el-input v-model="ruleForm.name" placeholder="请输入黑名单姓名" maxlength="255" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="黑名单证件号" prop="idCard">
							<el-input v-model="ruleForm.idCard" placeholder="请输入黑名单证件号" maxlength="255" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="证件照片" prop="idCardImg">
							<el-upload
							list-type="picture-card"
							:limit="1"
							:show-file-list="false"
							ref="uploadIdCardImgRef"
							:on-exceed="handleExceedIdCardImg"
							:before-upload="handleBeforeUploadIdCardImg"
							:auto-upload="true"
							:http-request="uploadIdCardImgHandle">
								<img
								v-if="ruleForm.idCardImg"
								:src="ruleForm.idCardImg"
								style="width: 100%; height: 100%; object-fit: contain"/>
								<el-icon v-else><Plus /></el-icon>
								
							</el-upload>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="Flow系统公司名称" prop="company">
							<el-select clearable filterable v-model="ruleForm.company" placeholder="请选择Flow系统公司名称" @change="CompanyChange">
								<el-option v-for="(item,index) in flowSystemSGCompanyDropdownList" :key="index" :value="item.value" :label="item.label" />
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="限制开始时间" prop="startTime">
							<el-date-picker v-model="ruleForm.startTime" type="date" placeholder="限制开始时间" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="限制结束时间" prop="endTime">
							<el-date-picker v-model="ruleForm.endTime" type="date" placeholder="限制结束时间" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否启用" prop="isEnabled">
							<el-switch v-model="ruleForm.isEnabled" active-text="是" inactive-text="否" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="ruleForm.remark" placeholder="请输入备注" type="textarea"  show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { ref,onMounted,nextTick } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import _ from 'lodash';
	import { genFileId } from 'element-plus';
	import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
    import { Plus,UploadFilled } from "@element-plus/icons-vue";
    import { UploadRequestOptions } from "element-plus";
    import {uploadIdCardImg} from '/@/api/main/vistLogBlack';
	import { addVistLogBlack, updateVistLogBlack } from "/@/api/main/vistLogBlack";
	import { getFlowSystemSGCompanyDropdown } from '/@/api/main/vistLogBlack';

	
	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	   const uploadIdCardImgRef = ref();
	   const idCardImgArrs=ref([]);
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
	const timer=ref<any>();
	let mianPageData=ref<any>();
	//自行添加其他规则
	const rules = ref<FormRules>({
		name: [{required: true, message: '请输入黑名单姓名！', trigger: 'blur',},],
		idCard: [{required: true, message: '请输入黑名单证件号！', trigger: 'blur',},],
	});

	// 打开弹窗
	const openDialog = (row: any,notParentCall=false) => {
	    mianPageData.value = row
	    timer.value = new Date().getTime();
		ruleForm.value.$TemplateFieldDisabled = notParentCall;
		if(row!=null && notParentCall ==false){
		  ruleForm.value = JSON.parse(JSON.stringify(row));
		  }
		else {
			//作為新增明細時候的組件使用
			if(row.id!=undefined &&notParentCall==true){
			  ruleForm.value.$TemplateField =row.id;
			}
		}
		isShowDialog.value = true;
			getCompanyDropdownList();
		//新增的时候需要给集合字段赋值为： []
		if(row.id==undefined){
		}
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = _.throttle( async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addVistLogBlack(values);
				} else {
					await updateVistLogBlack(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	},2000);


	const CompanyChange = async (changeItem) => {
		var item= flowSystemSGCompanyDropdownList.value.find(r=>r.value == changeItem);
		if(item!=null){
		}
	  }
	const flowSystemSGCompanyDropdownList = ref<any>([]); 
	const getCompanyDropdownList = async () => {
	    var model=ruleForm.value;
	    let list = await getFlowSystemSGCompanyDropdown(model);
		flowSystemSGCompanyDropdownList.value = list.data.result ?? [];
	};
	




	const uploadIdCardImgHandle = async (options: UploadRequestOptions) => {
		const res = await uploadIdCardImg(options);
		ruleForm.value.idCardImg = res.data.result?.url;
	};
	const handleExceedIdCardImg: UploadProps['onExceed'] = (files) => {
		nextTick(() => {
	 		 uploadIdCardImgRef.value!.clearFiles();
	   		const file = files[0];
	   		file.uid = genFileId();
	   		uploadIdCardImgRef.value!.handleStart(file);
	 		uploadIdCardImgRef.value!.submit();
	   	});	
	};
	 const handleBeforeUploadIdCardImg = (file) => {
			 const maxSize = 3 * 1024 * 1024;  // 设置文件大小限制为3MB
			 if (file.size > maxSize) {
				 ElMessage({
					 message: `${file.name}-文件大小超过了${maxSize / (1024 * 1024)}MB的限制`,
					 type: "error",
				 });
				 return false;  // 阻止文件上传
			 }
			 return true;  // 允许文件上传
		 }

	

	const checkNeedDisable =(previousColumn)=>{
	     if(typeof previousColumn !== 'undefined' && previousColumn != null && previousColumn !== ''&&previousColumn>0){
		   return false;
			}
	   return true;
	}

	// 页面加载时
	onMounted(async () => {
	});

	const commentClick =async (row : any) => {
		timer.value = new Date().getTime();
		//ruleForm.value.standardId = row.id;
		isShowDialog.value = true;
	};

	//将属性或者函数暴露给父组件
	defineExpose({ openDialog,commentClick });
</script>




